import { Application, connector, createApp, Session } from "mydog";
import { I_matchRole } from "./app/svr_match/matchMgr";
import { I_roleInfo } from "./app/util/someInterface";
import { cardUtil, e_card_arr_type, e_card_color, e_card_score, I_card, I_cardRes } from "./app/svr_game/cardLogic";
import { Room, Player, e_roomState } from "./app/svr_game/room";

let cards = cardUtil.genCards(2, true)

// cards = cards.concat(cardUtil.getCards())

let current_color = e_card_color.red

// for (let i = 0; i < cards.length; i++) {
//     console.log(cardUtil.toString(cards[i]))
// }

cards.sort((a, b) => {
    return cardUtil.show_compare(a, b, current_color);
    // return a.score < b.score ? 1 : -1;
});

let id = 0;
let firstCardsList: I_card[] = [{ "id": 100, "color": e_card_color.black, "score": e_card_score.c6 }, { "id": 101, "color": e_card_color.black, "score": e_card_score.c6 }];
let masterColor = e_card_color.black
let cardsInHand: I_card[] = []
cardsInHand.push({ "id": 1, "color": e_card_color.rect, "score": e_card_score.c3})
// cardsInHand.push({ "id": 2, "color": e_card_color.rect, "score": e_card_score.c3})
// cardsInHand.push({ "id": 3, "color": e_card_color.rect, "score": e_card_score.c7})
// cardsInHand.push({ "id": 4, "color": e_card_color.flower, "score": e_card_score.c7})
// // cardsInHand.push({ "id": 5, "color": e_card_color.red, "score": e_card_score.c7})
// cardsInHand.push({ "id": 6, "color": e_card_color.black, "score": e_card_score.c7})

cardsInHand.push({ "id": 7, "color": e_card_color.black, "score": e_card_score.c9})
// cardsInHand.push({ "id": 8, "color": e_card_color.black, "score": e_card_score.c8})

// cardsInHand.push({ "id": 9, "color": e_card_color.flower, "score": e_card_score.c8})

cardsInHand.push({ "id": 10, "color": e_card_color.red, "score": e_card_score.c8})
// cardsInHand.push({ "id": 11, "color": e_card_color.red, "score": e_card_score.c8})

// cardsInHand.push({ "id": 12, "color": e_card_color.black, "score": e_card_score.c8})


let selectedCards: I_card[] = [{ "id": 8, "color": e_card_color.black, "score": e_card_score.c8}];

let wantSelectCard= { "id": 10, "color": e_card_color.red, "score": e_card_score.c8 };

// console.log(cardUtil.can_be_select(firstCardsList, masterColor, cardsInHand, selectedCards, wantSelectCard))

let cardsInRound = [
    [{ "id": 8,"color": e_card_color.rect, "score": e_card_score.c6}, { "id": 8,"color": e_card_color.rect, "score": e_card_score.c6}],
    [{ "id": 8,"color": e_card_color.black, "score": e_card_score.c5}, { "id": 8,"color": e_card_color.black, "score": e_card_score.c5}],
    [{ "id": 8,"color": e_card_color.rect, "score": e_card_score.c10}, { "id": 8,"color": e_card_color.rect, "score": e_card_score.c10}],
    [{ "id": 8,"color": e_card_color.red, "score": e_card_score.c2}, { "id": 8,"color": e_card_color.black, "score": e_card_score.c2}],
 ]
// console.log('winner is :' + cardUtil.get_winner(cardsInRound, masterColor))
// console.log('points in round is :' + cardUtil.get_points(cardsInRound))

// let app = createApp();
// app.setConfig("rpc", { interval: 30 });
// app.start();

function userSetMasterColor(room: Room, uid: number, card: I_card) {

}

let app = createApp();
let room = new Room(app, id);
// // uid: number,
// // sid: string,
// // nickname: string,
// // gold: number
let roles: I_matchRole[] = [
    {uid: 1, sid: "1", nickname: "1", gold: 1},
    {uid: 2, sid: "2", nickname: "2", gold: 2},
    {uid: 3, sid: "3", nickname: "3", gold: 3},
    {uid: 4, sid: "4", nickname: "4", gold: 4},
];
room.init(roles);




for(let i=0; i < 24; i++) {
    room.deal(1);
    room.deal(2);
    room.deal(3);
    room.deal(4);

    for (let j = 1; j <=4; j++) {
        let player = room.getPlayer(j);
        let card = player.cards.find(c => c.score == e_card_score.c7)
        // console.log(card?.score)
        if (card) {
            room.setMasterColor(j, card.id)
        }
    }
    
}

room.setState(e_roomState.decideDizhu)

let nowUid = room.getNowUid()

function gen(id: number) {
    if (id == 0) {
        return 4;
    }

    return id
}

room.qiangDizhu(gen(nowUid%roles.length), 105); nowUid++
room.qiangDizhu(gen(nowUid%roles.length), -1); nowUid++
room.qiangDizhu(gen(nowUid%roles.length), 90); nowUid++
room.qiangDizhu(gen(nowUid%roles.length), 45); nowUid++

// for (let i = 0; i < roles.length; i++) {
//     let player = room.getPlayer(roles[i].uid);
//     // console.log(player.toClientJson())
// }

room.setState(e_roomState.normal)

for(let i=0; i < 24; i++) {
    room.autoPlay();
    room.autoPlay();
    room.autoPlay();
    room.autoPlay();
}



// let player: Player = room.getPlayer(1);
// // console.log(player.toClientJson())

// room.playCard(1, [1], () => { });

// let cardres = room.getCurrentPlayCardResList();
// // console.log(JSON.stringify(cardres))

// player = room.getPlayer(2);
// // console.log(player.toClientJson())
// room.playCard(2, [38], (e: any) => { 
//     console.log(JSON.stringify(e))
// });
// cardres = room.getCurrentPlayCardResList();
// // console.log(JSON.stringify(cardres))

// player = room.getPlayer(3);
// // console.log(player.toClientJson())
// room.playCard(3, [64], (e: any) => { 
//     console.log(JSON.stringify(e))
// });

// cardres = room.getCurrentPlayCardResList();
// // console.log(JSON.stringify(cardres))

// player = room.getPlayer(4);
// console.log(player.toClientJson())
// room.playCard(4, [105], (e: any) => { 
//     console.log(JSON.stringify(e))
// });

// cardres = room.getCurrentPlayCardResList();
// console.log(JSON.stringify(cardres,null, '  '))